<?php

namespace App\Model;

use App\Http\Controllers\Constant\Constant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;

/**
 * @property string $dept_name
 * @property string $company_name
 * @property string $name
 * @property string $leader
 * @property string $leader_mobile
 * @property string $desc
 * @property string $cost
 * @property string $offer
 * @property integer $partner_id
 * @property integer $uid
 * @property string $start
 * @property string $stop
 * @property integer $node_id
 * @property integer $gsid
 * @property integer $deptid
 * @property string $batch_id
 * @property integer $is_save
 * @property string $currency
 * @property integer $roleid
 * @property string $city
 * @property string $seller
 * @property string $seller_mobile
 * @property string $customer
 * @property string $customer_mobile
 * @property integer $work_id
 * @property string $status
 * @property string $approver
 * @property string $create_role
 * @property string $role_list
 * @property BelongsTo $department
 * @property BelongsTo $gongsi
 */
class OAProjectLx extends OABaseModel
{
    protected $table = 'project_lx';

    public $timestamps = true;

    protected $fillable = [
        'name',
        'leader',
        'leader_mobile',
        'desc',
        'cost',
        'offer',
        'partner_id',
        'uid',
        'start',
        'stop',
        'node_id',
        'gsid',
        'deptid',
        'batch_id',
        'is_save',
        'currency',
        'roleid',
        'city',
        'seller',
        'seller_mobile',
        'customer',
        'customer_mobile',
        'work_id',
        'status',
        'approver',
        'create_role',//创建者身份
        'role_list',//可查看角色
    ];
    protected $hidden = ['department', 'gongsi'];
    protected $attributes = ['approver' => 'array','role_list'=>"array"];
    protected $casts = ['approver' => 'array','role_list'=>"array"];

    //删除项目
    public static function delProjectXm($id)
    {
        return self::query()->where('id', $id)->delete();
    }

    public function getOfferAttribute($offer)
    {
        return number_format(resetMoney($offer), 2);
    }

    public function getCostAttribute($cost)
    {
        return number_format(resetMoney($cost), 2);
    }

    public function getCreatedAtAttribute($created_at)
    {
        return Carbon::parse($created_at)->format("Y-m-d");
    }

    public function ys()
    {
        return $this->hasMany(OAProjectYs::class, 'xmid');
    }

    public function cw()
    {
        return $this->hasMany(OAFinance::class, 'xmid');
    }

    public function ysDetail()
    {
        return $this->hasMany(OAProjectYsDetail::class, 'xmid');
    }

    public function department(): BelongsTo
    {
        return $this->belongsTo(OADept::class, 'deptid');
    }

    public function gongsi(): BelongsTo
    {
        return $this->belongsTo(OACompany::class, 'gsid');
    }

    public function getDeptNameAttribute()
    {
        return optional($this->department)->title ?? '';
    }

    public function getCompanyNameAttribute()
    {
        return optional($this->gongsi)->name ?? '';
    }

    public function work()
    {
        return $this->belongsTo(OAWork::class, 'work_id');
    }

    public static function createQueryBuilder(array $options = [])
    {
        return self::query()
            ->roles()
            ->status(Arr::get($options, 'status',0))
            ->when($name = Arr::get($options, 'name', ''), function ($query) use ($name) {
                return $query->where('name', 'like', '%' . $name . '%');
            })
            ->when($start = Arr::get($options, 'start', ''), function ($query) use ($start) {
                return $query->where('start', '>=', $start);
            })
            ->when($stop = Arr::get($options, 'stop', ''), function ($query) use ($stop) {
                return $query->where('stop', '<=', $stop);
            })
            ->when($batch_id = Arr::get($options, 'batch_id', ''), function ($query) use ($batch_id) {
                return $query->where('batch_id', 'like', '%' . $batch_id . '%');
            })
            ->orderBy('created_at','DESC')
            ->latest("updated_at");

    }




}
